package com.purplesoft.zettl.Activities;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.ListView;
import com.purplesoft.zettl.R;
import com.purplesoft.zettl.Adapters.AutoCompleteItemListAdapter;
import com.purplesoft.zettl.Adapters.ItemListAdapter;
import com.purplesoft.zettl.Listener.ZettlGestureDetector;
import com.purplesoft.zettl.Provider.DatabaseContentProvider;
import com.purplesoft.zettl.Utilities.AutoCompleteTextViewBackEvent;

public class Zettl extends Activity {

	private ListView lvZettl;
	private AutoCompleteTextViewBackEvent acAddNewItem;
	private ImageView btAddNewItem, btAddNewList;
	
	private ItemListAdapter laItems;
	private AutoCompleteItemListAdapter laAcItems;
	private Cursor csVwPurchaseItems, csVwItems;
	
	private InputMethodManager softKeyboard;
	
	private GestureDetector gestureDetector;
	private View.OnTouchListener gestureListener;
	
	private static final String LIST_STATE = "listState";
	private Parcelable mListState = null;


	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTheme(R.style.Theme_Purple);
		setContentView(R.layout.zettl);

		// Bilde Referenzen auf die einzelnen Widgets von zettl.xml
		lvZettl = (ListView) findViewById(R.id.lvListItems_Zettl);
		acAddNewItem = (AutoCompleteTextViewBackEvent) findViewById(R.id.acAddNewItem_Zettl);
		btAddNewItem = (ImageView) findViewById(R.id.btAddNewItem_Zettl);
		btAddNewList = (ImageView) findViewById(R.id.btAddNewList_Zettl);
		
		// Populate list with items from vwPurchaseItems
		csVwPurchaseItems = getContentResolver().query(
				DatabaseContentProvider.VWPurchaseItems, null, null,
				null, null);
//		startManagingCursor(csVwPurchaseItems);
		laItems = new ItemListAdapter(this, csVwPurchaseItems);
		lvZettl.setAdapter(laItems);
		
		// Populate acAddNewItem with items from vwItems
		csVwItems = getContentResolver().query(
				DatabaseContentProvider.VWItems, null, null,
				null, null);
		laAcItems = new AutoCompleteItemListAdapter(this, csVwItems, acAddNewItem,
				btAddNewItem, btAddNewList, lvZettl);
		acAddNewItem.setAdapter(laAcItems);
		
		// Setze einen OnKeyListener und OnItemClickListener auf acAddNewItem
		acAddNewItem.setOnKeyListener(laAcItems);
		acAddNewItem.setOnItemClickListener(laAcItems);
		
		
		gestureDetector = new GestureDetector(new ZettlGestureDetector(this, lvZettl));
		gestureListener = new View.OnTouchListener() {
			
			public boolean onTouch(View v, MotionEvent event) {
				return gestureDetector.onTouchEvent(event);
			}
		};
		
		lvZettl.setOnTouchListener(gestureListener);
	}
	
	public void zettlOnClickHandler(View view) {
		switch (view.getId()) {
		case R.id.btAddNewItem_Zettl:
			acAddNewItem.setVisibility(View.VISIBLE);
			btAddNewItem.setVisibility(View.INVISIBLE);
			btAddNewList.setVisibility(View.INVISIBLE);
			acAddNewItem.requestFocus();
			softKeyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
			softKeyboard.showSoftInput(acAddNewItem, 0);
		case R.id.btEditList_Zettl:
			// don't know for what this is?
//			Intent intent = new Intent(this, ManageLists.class);
//			intent.putExtra(VwPurchaseItems.Columns., attribute_result);
//			startActivityForResult(intent, attribute_result);
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		csVwPurchaseItems = getContentResolver().query(
				DatabaseContentProvider.VWPurchaseItems, null, null,
				null, null);
		laItems = new ItemListAdapter(this, csVwPurchaseItems);
		lvZettl.setAdapter(laItems);
		if (mListState != null)
	        lvZettl.onRestoreInstanceState(mListState);
	    mListState = null;
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		mListState = savedInstanceState.getParcelable(LIST_STATE);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		mListState = lvZettl.onSaveInstanceState();
		outState.putParcelable(LIST_STATE, mListState);
	}

	
}